<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class admin_service{
	private $CI;
	function __construct() { 
		$this->CI = &get_instance();
		$this->CI->load->model('Admin_model');
	}
	

	/**  
	* 根据用户id 取出用户
	* @access public 
	* @param  $del 是否删除 ，默认不删除
	* @param  $is_teacher 是否教师
	* @return array
	*/  
	public function getAllAdmin($del=false,$is_teacher=true,$teacher_name=false) {
		$cond=array();
		if(!$del){
			$cond['status']=CommonStatus::UnDeleted[XPHEnum::Value];
		}
		$connar['cond']=$cond;
		$connar['where']=$this->CI->sqlEscape(" role_id<>?? ",array(WebRole::Admin[XPHEnum::Value]));
		if($is_teacher){
			$connar['where'].=$this->CI->sqlEscape(" and (role_id=?? or FIND_IN_SET(??,sub_role_id)) ",array(WebRole::Teacher[XPHEnum::Value],WebRole::Teacher[XPHEnum::Value]));
		}
		if($teacher_name){
			$connar['where'] .= "and (username like '%".$teacher_name."%' or usercode like '%".$teacher_name."%')";
		}
		$admin=$this->CI->Admin_model->get_list_full($connar);
		return $admin;
	}

	/**  
	* 根据用户id 取出用户
	* @access public 
	* @param  $dept_name 部门名称
	* @param  $del 是否删除 ，默认不删除
	* @param  $is_teacher 是否教师
	* @return array
	*/  
	public function getAdminByDeptName($dept_name,$del=false,$is_teacher=true) {
		$cond=array('dept_name'=>$dept_name);
		if(!$del){
			$cond['status']=CommonStatus::UnDeleted[XPHEnum::Value];
		}
		$connar['cond']=$cond;
		$connar['where']=$this->CI->sqlEscape(" role_id<>?? ",array(WebRole::Admin[XPHEnum::Value]));
		if($is_teacher){
			$connar['where'].=$this->CI->sqlEscape(" and (role_id=?? or FIND_IN_SET(??,sub_role_id)) ",array(WebRole::Teacher[XPHEnum::Value],WebRole::Teacher[XPHEnum::Value]));
		}
		$admin=$this->CI->Admin_model->get_list_full($connar);
		return $admin;
	}

	/**  
	* 根据用户id 取出用户
	* @access public 
	* @param  $admin_id 用户id
	* @return array
	*/  
	public function getAdminById($admin_id) {
		$admin=$this->CI->Admin_model->get_single(array('id'=>$admin_id));
		return $admin;
	}

	/**  
	* 取出所有指定角色用户
	* @access public 
	* @param  $role 角色
	* @param  $field 要取的字段，如果传递，将拼出一个字段的一位数组
	* @return array
	*/  
	public function getAdminByRole($role,$field=false) {
		$sql=" select * from db_admin where (role_id=".$role." or FIND_IN_SET(".$role.",sub_role_id)) and status=".CommonStatus::UnDeleted[XPHEnum::Value];
		$admin=$this->CI->Admin_model->query($sql);
		if($admin){
			if($field){
				return array_column($admin,$field);
			}
			else{
				return $admin;
			}
		}
		else{
			if($field){
				return array();
			}
			else{
				return $admin;
			}
		}
	}

	/**  
	* 取出所有指定多角色用户
	* @access public 
	* @param  $role_array 角色
	* @param  $field 要取的字段，如果传递，将拼出一个字段的一位数组
	* @return array
	*/  
	public function getAdminByRoleArray($role_array,$field=false) {
		$sql=$this->CI->sqlEscape(" select * from db_admin where status=?? and ( ",array(CommonStatus::UnDeleted[XPHEnum::Value]));
		foreach ($role_array as $k => $v) {
			if($k!=0){
				$sql.=" or ";
			}
			$sql.=$this->CI->sqlEscape(" (role_id=?? or FIND_IN_SET(??,sub_role_id)) ",array($v,$v));
		}
		$sql.=" ) ";
		$admin=$this->CI->Admin_model->query($sql);
		if($admin){
			if($field){
				return array_column($admin,$field);
			}
			else{
				return $admin;
			}
		}
		else{
			if($field){
				return array();
			}
			else{
				return $admin;
			}
		}
	}
	
	
}